//
//  MainViewController.m
//  TrunkIndustryManageSystem
//
//  Created by 李璐西 on 15-3-26.
//  Copyright (c) 2015年 李璐西. All rights reserved.
//

#import "MainViewController.h"
#import "BiNetEngine.h"
#import "ViewTestControllerViewController.h"


@interface MainViewController ()<UIScrollViewDelegate, BiNetEngineDelegate>
@property (nonatomic, strong) NSArray *imageList;
@end

@implementation MainViewController

- (NSArray *)imageList
{
    if (_imageList == nil)
    {
        NSDictionary *dict1 = @{@"name": @"main/icon_main_vehiclepurchase.png", @"title": @"整车采购",@"target":@"TestClicked"};
        NSDictionary *dict2 = @{@"name": @"main/icon_main_vehicledisassembly.png", @"title": @"整车拆装",@"target":@"onTextViewButtonClicked:"};
        NSDictionary *dict3 = @{@"name": @"main/icon_main_vehiclebyvehicle.png", @"title": @"整车借车",@"target":@"onTextViewButtonClicked:"};
        NSDictionary *dict4 = @{@"name": @"main/icon_main_vehiclesales.png", @"title": @"整车销售",@"target":@"onTextViewButtonClicked:"};
        NSDictionary *dict5 = @{@"name": @"main/icon_main_roadtransportpermits.png", @"title": @"道路运输证",@"target":@"onTextViewButtonClicked:"};
        NSDictionary *dict6 = @{@"name": @"main/icon_main_twomaintenancecertificate.png", @"title": @"二级维护证",@"target":@"onTextViewButtonClicked:"};
        NSDictionary *dict7 = @{@"name": @"main/icon_main_hazardouscertificate.png", @"title": @"危化证",@"target":@"onTextViewButtonClicked:"};
        NSDictionary *dict8 = @{@"name": @"main/icon_main_drivinglicense.png", @"title": @"行驶证",@"target":@"onTextViewButtonClicked:"};
        NSDictionary *dict9 = @{@"name": @"main/icon_main_mortgage.png", @"title": @"按揭",@"target":@"onTextViewButtonClicked:"};
        NSDictionary *dict10 = @{@"name": @"main/icon_main_borrowshort.png", @"title": @"短借",@"target":@"onTextViewButtonClicked:"};
        NSDictionary *dict11 = @{@"name": @"main/icon_main_insure.png", @"title": @"投保",@"target":@"onTextViewButtonClicked:"};
        NSDictionary *dict12 = @{@"name": @"main/icon_main_receivedclearance.png", @"title": @"清收",@"target":@"onTextViewButtonClicked:"};
        NSDictionary *dict13 = @{@"name": @"main/icon_main_claims.png", @"title": @"理赔",@"target":@"onTextViewButtonClicked:"};
        NSDictionary *dict14 = @{@"name": @"main/icon_main_service.png", @"title": @"维修",@"target":@"onTextViewButtonClicked:"};
        NSDictionary *dict15 = @{@"name": @"main/icon_main_partsprocurement.png", @"title": @"配件采购",@"target":@"onTextViewButtonClicked:"};
        NSDictionary *dict16 = @{@"name": @"main/icon_main_partssales.png", @"title": @"配件销售",@"target":@"onTextViewButtonClicked:"};
        _imageList = @[dict1, dict2, dict3, dict4, dict5, dict6, dict7, dict8, dict9, dict10, dict11, dict12, dict13, dict14, dict15, dict16];
    }
    return _imageList;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    [self.navigationController setNavigationBarHidden:YES animated:NO];
    
    // 模拟请求
    [[BiNetEngine shareInstance] requestCarSaleCheckList:self index:0];
    
    _naviBarView = [[BiNaviBarView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
    [self.view addSubview:_naviBarView];
    [_naviBarView setHidden:YES];
    [_naviBarView setTitle:@""];
    
//    [self setBackgroundColor:UIColorFromRGB(0x1ca0ff)];

#pragma mark ScrollView
    CGRect frame = [self.view bounds];
    UIScrollView *background = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 20, 320, frame.size.height)];
    [self.view addSubview:background];
    background.backgroundColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:100];
//    滚动
    background.contentSize = CGSizeMake(320, frame.size.height+1);
    background.delegate = self;
#pragma mark button
    
#define AppViewW MAIN_SCREEN_WIDTH/4
#define AppViewH MAIN_SCREEN_HEIGHT/5
#define ColCount 4
// 由一个button循环出
    for (int i = 0; i < 16; i++)
    {
        // 0,0    80,0   160,0   240,0    => i = 0
        // 0,110  80,110 160,110 240,110  => i = 1
        // 0,220  80,220 160,220 240,220  => i = 2
        // 0,330  80,330 160,330 240,330  => i = 3
        int row = i / 4;
        int col = i % 4;
        CGFloat x = col * AppViewW;
        CGFloat y = row * AppViewH;
//        button
    UIButton * Btn = GET_BUTTON(CGRectMake(x, y, AppViewW, AppViewH), 17, NO, [UIColor whiteColor]);
    Btn.titleLabel.font = [UIFont systemFontOfSize:14];
    [Btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
//    [Btn setTitleColor:[UIColor colorWithRed:10.0/255.0 green:115.0/255.0 blue:190.0/255.0 alpha:100] forState:UIControlStateHighlighted];
    [Btn setTitleEdgeInsets:UIEdgeInsetsMake(55, -38, 0, 0)];
    [Btn setTitle:self.imageList[i][@"title"] forState:UIControlStateNormal];
//        NSString * imagePath = self.imageList[i][@"name"];
//        UIImage * image = LOAD_IMAGE_WITH_CACHE(self.imageList[i][@"name"]);
        [Btn setImage:LOAD_IMAGE_WITH_CACHE(self.imageList[i][@"name"]) forState:UIControlStateNormal];
        [Btn setImage:LOAD_IMAGE_WITH_CACHE(self.imageList[i][@"name"]) forState:UIControlStateHighlighted];
   
//        [Btn setImage:[UIImage imageNamed:self.imageList[i][@"name"]] forState:UIControlStateNormal];
//        [Btn setImage:[UIImage imageNamed:self.imageList[i][@"name"]] forState:UIControlStateHighlighted];
        
//        NSString *imgPath = [[NSBundle mainBundle] pathForResource:@"resource/main/icon_31" ofType:@"png" ];
        
     
      //  [Btn setImage:self.imageList[i][@"name"] forState:UIControlStateNormal];
    Btn.imageView.contentMode = UIViewContentModeCenter;
    [Btn setImageEdgeInsets:UIEdgeInsetsMake(-16, 17, 0, 0)];
        
        Btn.tag =i;
        
        UIColor *color =   [UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:100];
        UIImage *img = [self createImageWithColor:color];
        
        [Btn setBackgroundImage:img  forState:UIControlStateHighlighted];
//    [Btn1 setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
        
    Btn.layer.borderColor = [[UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:100] CGColor];
    Btn.layer.borderWidth = 0.5f;
    [Btn addTarget:self action:@selector(onTextViewButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [background addSubview:Btn];
    }
// ****************************************************
    if (isIOS7)
    {
        [_naviBarView setFrame:CGRectMake(0, 0, _naviBarView.frame.size.width, 64)];
        _naviBarView.tintColor = [UIColor whiteColor];
    }
    else
    {
        [_naviBarView setFrame:CGRectMake(0, -20, _naviBarView.frame.size.width, 64)];
        _naviBarView.tintColor = [UIColor whiteColor];
    }
    [_naviBarView release];
    
    self.view.backgroundColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:100];
#pragma mark BottomView
    UIView *bottomLine = [[UIView alloc]initWithFrame:CGRectMake(0, MAIN_SCREEN_HEIGHT-88, MAIN_SCREEN_WIDTH, 0.5)];
    [self.view addSubview:bottomLine];
    bottomLine.backgroundColor = [UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:100];
#pragma mark BottomView button
    UIView *bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, MAIN_SCREEN_HEIGHT-87, MAIN_SCREEN_WIDTH, 88)];
    [self.view addSubview:bottomView];
    bottomView.backgroundColor = [UIColor whiteColor];
    
    UIButton * Btn1 = GET_BUTTON(CGRectMake(0, -7, AppViewW, 88), 17, NO, [UIColor whiteColor]);
    Btn1.titleLabel.font = [UIFont systemFontOfSize:12];
    [Btn1 setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [Btn1 setTitleColor:[UIColor colorWithRed:10.0/255.0 green:115.0/255.0 blue:190.0/255.0 alpha:100] forState:UIControlStateHighlighted];
    [Btn1 setTitleEdgeInsets:UIEdgeInsetsMake(55, -45, 0, 0)];
    [Btn1 setTitle:@"个人中心" forState:UIControlStateNormal];
    [Btn1 setImage:LOAD_IMAGE_WITH_CACHE(@"main/bottom/icon_main_user_p.png") forState:UIControlStateHighlighted];
    [Btn1 setImage:LOAD_IMAGE_WITH_CACHE(@"main/bottom/icon_main_user.png") forState:UIControlStateNormal];
    Btn1.imageView.contentMode = UIViewContentModeCenter;
    [Btn1 setImageEdgeInsets:UIEdgeInsetsMake(-16, 17, 0, 0)];
//    Btn1.backgroundColor = [UIColor blueColor];
    [bottomView addSubview:Btn1];
    
    UIButton * Btn2 = GET_BUTTON(CGRectMake(80, -7, AppViewW, 88), 17, NO, [UIColor whiteColor]);
    Btn2.titleLabel.font = [UIFont systemFontOfSize:12];
    [Btn2 setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [Btn2 setTitleColor:[UIColor colorWithRed:10.0/255.0 green:115.0/255.0 blue:190.0/255.0 alpha:100] forState:UIControlStateHighlighted];
    [Btn2 setTitleEdgeInsets:UIEdgeInsetsMake(55, -45, 0, 0)];
    [Btn2 setTitle:@"行政" forState:UIControlStateNormal];
    [Btn2 setImage:LOAD_IMAGE_WITH_CACHE(@"main/bottom/icon_main_administration_h.png") forState:UIControlStateHighlighted];
    [Btn2 setImage:LOAD_IMAGE_WITH_CACHE(@"main/bottom/icon_main_administration.png") forState:UIControlStateNormal];
    Btn2.imageView.contentMode = UIViewContentModeCenter;
    [Btn2 setImageEdgeInsets:UIEdgeInsetsMake(-16, 17, 0, 0)];
    //    Btn2.backgroundColor = [UIColor blueColor];
    [bottomView addSubview:Btn2];
    
    UIButton * Btn3 = GET_BUTTON(CGRectMake(160, -7, AppViewW, 88), 17, NO, [UIColor whiteColor]);
    Btn3.titleLabel.font = [UIFont systemFontOfSize:12];
    [Btn3 setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [Btn3 setTitleColor:[UIColor colorWithRed:10.0/255.0 green:115.0/255.0 blue:190.0/255.0 alpha:100] forState:UIControlStateHighlighted];
    [Btn3 setTitleEdgeInsets:UIEdgeInsetsMake(55, -45, 0, 0)];
    [Btn3 setTitle:@"人事" forState:UIControlStateNormal];
    [Btn3 setImage:LOAD_IMAGE_WITH_CACHE(@"main/bottom/icon_main_personnel_h.png") forState:UIControlStateHighlighted];
    [Btn3 setImage:LOAD_IMAGE_WITH_CACHE(@"main/bottom/icon_main_personnel.png") forState:UIControlStateNormal];
    Btn3.imageView.contentMode = UIViewContentModeCenter;
    [Btn3 setImageEdgeInsets:UIEdgeInsetsMake(-16, 17, 0, 0)];
    //    Btn3.backgroundColor = [UIColor blueColor];
    [bottomView addSubview:Btn3];
    
    UIButton * Btn4 = GET_BUTTON(CGRectMake(240, -7, AppViewW, 88), 17, NO, [UIColor whiteColor]);
    Btn4.titleLabel.font = [UIFont systemFontOfSize:12];
    [Btn4 setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [Btn4 setTitleColor:[UIColor colorWithRed:10.0/255.0 green:115.0/255.0 blue:190.0/255.0 alpha:100] forState:UIControlStateHighlighted];
    [Btn4 setTitleEdgeInsets:UIEdgeInsetsMake(55, -45, 0, 0)];
    [Btn4 setTitle:@"报表" forState:UIControlStateNormal];
    [Btn4 setImage:LOAD_IMAGE_WITH_CACHE(@"main/bottom/icon_main_forms_h.png") forState:UIControlStateHighlighted];
    [Btn4 setImage:LOAD_IMAGE_WITH_CACHE(@"main/bottom/icon_main_forms.png") forState:UIControlStateNormal];
    Btn4.imageView.contentMode = UIViewContentModeCenter;
    [Btn4 setImageEdgeInsets:UIEdgeInsetsMake(-16, 17, 0, 0)];
    //    Btn4.backgroundColor = [UIColor blueColor];
    [bottomView addSubview:Btn4];
}

- (void)onGetCarSaleCheckList:(id)array
{
    if ([array isKindOfClass:[NSArray class]])
    {
        // 处理网络数据
    }
}

#pragma mark -
#pragma mark BiNetEngineDelegate
- (void)onNetEngineResponse:(int)tag entity:(id)entity userInfo:(NSDictionary *)userInfo
{
    NSLog(@"onNetEngineResponse");
    
    switch (tag)
    {
        case NET_GET_CAR_SALE_CHECK_LIST:
        {
            [self onGetCarSaleCheckList:entity];
        }
            break;
            
        default:
            break;
    }
}
- (void)onNetEngineResponseFail:(int)tag entity:(id)entity userInfo:(NSDictionary *)userInfo
{
    NSLog(@"onNetEngineResponseFail");
}

#pragma mark -------------------
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    // any offset changes
    NSLog(@"%f",scrollView.contentOffset.y);//这里我们输出 scrollView的纵向 偏移量
}
// called on start of dragging (may require some time and or distance to move)
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    NSLog(@"scrollViewWillBeginDragging");
}
// called on finger up if the user dragged. decelerate is true if it will continue moving afterwards
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    NSLog(@"scrollViewDidEndDragging willDecelerate");
}
// called on finger up as we are moving
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
    NSLog(@"scrollViewWillBeginDecelerating");
}
// called when scroll view grinds to a halt
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSLog(@"scrollViewDidEndDecelerating");
}

#pragma mark -------------------Click

- (void)onTextViewButtonClicked:(UIButton*)button
{
     ViewTestControllerViewController *viewTestcontroller =[[ViewTestControllerViewController alloc] init];
   // UINavigationController *navite = [[UINavigationController alloc]initWithRootViewController:viewTestcontroller ];
    
   
    
    [self.navigationController pushViewController:viewTestcontroller animated:YES];
}
- (UIImage *)createImageWithColor:(UIColor *)color
        {
            CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
            UIGraphicsBeginImageContext(rect.size);
            CGContextRef context = UIGraphicsGetCurrentContext();
            CGContextSetFillColorWithColor(context, [color CGColor]);
            CGContextFillRect(context, rect);
            UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            
            return theImage;
        }
@end
